package com.lucun.es_transaction.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 验证码校验
     */
    public boolean selectCodeCompare(String email,String code){
        String codeRedis = redisTemplate.opsForValue().get(email);
        boolean hasPhone = hasPhone(email);
        if(!hasPhone){
            throw new IllegalArgumentException("验证码已过期，请重新获取");
        }
        boolean codeBoolean=false;
        if(code.equals(codeRedis)){
             codeBoolean=true;
        }
        return codeBoolean;
    }
    /**
     * 判断key是否存在
     * @param phone
     * @return
     */
    public boolean hasPhone(String phone){
        return redisTemplate.hasKey(phone);
    }
    /**
     * 获取key的过期时间
     */
    public long getExpire(String phone){
        Long expire = redisTemplate.getExpire(phone);
        return expire;
    }
}
